<?php

namespace app\admin\controller\xiservice;

use addons\xiservice\controller\WeixinMini;
use app\common\controller\Backend;
use think\exception\ThrowableError;
use think\Exception;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Refund extends Backend
{

    /**
     * Refund模型对象
     * @var \app\common\model\xiservice\Refund
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\xiservice\Refund;
        $this->view->assign("stautsList", $this->model->getStautsList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['order','service','user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','refund_no','refund_money','refund_reason','refund_images','content','status','confirm_time']);
                $row->visible(['order']);
				$row->getRelation('order')->visible(['order_no','total_money']);
				$row->visible(['service']);
				$row->getRelation('service')->visible(['name','image']);
				$row->visible(['user']);
				$row->getRelation('user')->visible(['username','avatar','mobile']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function agree($ids=null)
    {
        $refund = $this->model->where('id',$ids)->where('status',0)->find();
        if(!$refund){
             $this->error('订单信息错误');
        }

        $orderModel = new \app\common\model\xiservice\Order();
        $order = $orderModel->where('order_status',40)
                             ->where('is_refund',1)
                             ->where('id',$refund['order_id'])->find();
        if(!$order){
             $this->error('订单状态错误');
        }
        $res = false;
        Db::startTrans();
        try {  
            
            $data['status']  = 1;            
            $result = $this->model->allowField(true)->save($data,["id"=>$order['refund_id']]);
            $res = $orderModel->changeOrderStatus($refund['order_id'],'refund') ;
            if(!$res['status']){
                throw new Exception($res['msg']);
            }
            Db::commit();
        }catch (Exception $e){
             Db::rollback();
            $this->error($e->getMessage());
        }

        if(!$res){
            $this->error('操作失败');
        }
        $this->success('操作成功');  

    }

    public function refuse($ids=null)
    {
        $refund = $this->model->where('id',$ids)->where('status',0)->find();
        if(!$refund){
             $this->error('订单信息错误');
        }

        $orderModel = new \app\common\model\xiservice\Order();
        $order = $orderModel->where('order_status',40)
                             ->where('is_refund',1)
                             ->where('id',$refund['order_id'])->find();
        if(!$order){
             $this->error('订单状态错误');
        }
        $res = false;
        Db::startTrans();
        try {  
            
            $data['status']  = 2;            
            $result = $this->model->allowField(true)->save($data,["id"=>$order['refund_id']]);
            $orderdata['is_refund'] = 0;
            $orderdata['refund_id'] = 0;
            $res = $orderModel->where('id',$refund['order_id'])->update($orderdata);
            Db::commit();
        }catch (Exception $e){
             Db::rollback();
            $this->error($e->getMessage());
        }
        if(!$res){
            $this->error('操作失败');
        }
        $this->success('操作成功');        

    }
}
